<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

class Upload {
	var $file, $filename, $destination, $thumbnail, $maxsize, $filesize, $permissions, $extensions, $tmp_filename, $error_code, $message;

	function Upload($file = '', 
			$destination = '', 
			$thumbnail = true,
			$maxsize = 4097152,
			$permissions = '777', 
			$extensions = array('png','gif','jpg','jpeg','tif')) 
	{
		$this->error_code = 0;
		$this->message = '';
		$this->set_file($file);
		$this->set_destination($destination);
		$this->set_thumbnail((boolean)$thumbnail);
		$this->set_maxsize((int)$maxsize);
		$this->set_permissions($permissions);
		$this->set_extensions($extensions);
		$this->width_max = 500;
		$this->height_max = 500;
	}

	function parse()
	{
		$file = array();

		if (isset($_FILES[$this->file]))
		{
			$this->set_filename($_FILES[$this->file]['name']);
			$this->set_tmp_filename($_FILES[$this->file]['tmp_name']);
			$this->set_filesize($_FILES[$this->file]['size']);
		}

		if ($this->tmp_filename != null && strlen($this->tmp_filename) > 0 && $this->tmp_filename != 'none' && is_uploaded_file($this->tmp_filename))
		{
			//Check allowed Extension
			if (sizeof($this->extensions) > 0) {
				if (!in_array(strtolower(substr($this->filename, strrpos($this->filename, '.') + 1)), $this->extensions)) {
					$this->error_code = -10;
					$this->message = "ERROR_FILETYPE_NOT_ALLOWED";
					return false;
				}
			}

			//Not attempting to move out of destination directory
			$pos = strpos($this->filename, '..');
			if (!($pos === false)) {
				$this->error_code = -15;
				$this->message = "ERROR_UPLOAD_FILE_ATTACK";
			}

			//Check destination
			if (!is_writeable($this->destination)) {
				if (is_dir($this->destination)) {
					$this->error_code = -20;
					$this->message = "ERROR_DESTINATION_NOT_WRITEABLE";
				} else {
					$this->error_code = -30;
					$this->message = "ERROR_DESTINATION_DOES_NOT_EXIST";
				}
				return false;
			} 

			//Check filesize
			if ($this->filesize > $this->maxsize) {
				$this->error_code = -35;
				$this->message = "ERROR_FILE_TOO_LARGE";
				return false;
			}

			return true;
		} 
		
		$this->error_code = -1;
		$this->message = "WARNING_NO_FILE_UPLOADED";
		return false;
	}

	function save() {
		if (substr($this->destination, -1) != '/') $this->destination .= '/';

		$filename = $this->filename;
		if ($this->thumbnail) $filename = 'tmp-'. $filename;
		if (move_uploaded_file($this->tmp_filename, $this->destination . $filename)) 
		{
			if ($this->thumbnail) {
				if (file_exists ($this->destination . $this->filename)) unlink($this->destination . $this->filename);
				list($width, $height, $type) = getimagesize($this->destination . $filename);  // create new dimensions, keeping aspect ratio
				$ratio = $width/$height;
				$width_max = $this->width_max;
				$height_max = $this->height_max;


   				if ($width_max/$height_max > $ratio) {$width_max = floor($height_max*$ratio);} else {$height_max = floor($width_max/$ratio);}

				switch ($type)
				{case 1:   //   gif -> jpg
					$img_src = imagecreatefromgif($this->destination . $filename);
					break;
				case 2:   //   jpeg -> jpg
					$img_src = imagecreatefromjpeg($this->destination . $filename); 
					break;
				case 3:  //   png -> jpg
					$img_src = imagecreatefrompng($this->destination . $filename);
					break;
     				}
				$img_dst = imagecreatetruecolor($width_max, $height_max);  //  resample
				$bool = imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $width_max, $height_max, $width, $height);
				imagejpeg($img_dst, $this->destination . $this->filename);    //  save new image
				unlink($this->destination . $filename);  //  clean up image storage
				imagedestroy($img_src);        
				imagedestroy($img_dst);	
			}
			chmod($this->destination . $this->filename, $this->permissions);
			$this->message = "SUCCESS_FILE_SAVED_SUCCESSFULLY";
			return true;
		} 
		else 
		{
			$this->error_code = -40;
			$this->message = "ERROR_FILE_NOT_SAVED";
			return false;
		}
	}

	function set_file($file) {
		$this->file = $file;
	}

	function set_destination($destination) {
		$this->destination = $destination;
	}

	function set_maxsize($maxsize) {
		$this->maxsize = (int)$maxsize;
	}

	function set_permissions($permissions) {
		$this->permissions = octdec($permissions);
	}

	function set_filesize($filesize) {
		$this->filesize = (int)$filesize;
	}

	function set_filename($filename) {
		$this->filename = $filename;
	}

	function set_thumbnail($thumbnail) {
		$this->thumbnail = $thumbnail;
	}

	function set_tmp_filename($filename) {
		$this->tmp_filename = $filename;
	}

	function set_extensions($extensions) {
		if ($extensions != null) {
			if (is_array($extensions)) {
				$this->extensions = $extensions;
			} else {
				$this->extensions = array($extensions);
			}
		} else {
			$this->extensions = array();
		}
	}
}

?>